package com.offcn.spark.p3

import com.offcn.spark.p3.CombineByKey2.groupByKey
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Auther: BigData-LGW
 * @ClassName: AggregateByKey
 * @Date: 2020/12/7 21:39
 * @功能描述: $FunctionDescription
 * @Version:1.0
 */
object AggregateByKey {
    def main(args: Array[String]): Unit = {
        val conf = new SparkConf()
            .setMaster("local[*]")
            .setAppName("AggregateByKey")
        val sc = new SparkContext(conf)
//        groupByKey(sc)
        reduceBy(sc)
        sc.stop()
    }
    def reduceBy(sc: SparkContext)={
        val array = sc.parallelize(Array(
            "hello you",
            "hello me",
            "hello you",
            "hello you",
            "hello me",
            "hello you"
        ), 2)
        val pairs = array.flatMap(_.split("\\s+")).map((_,1))
        val ret = pairs.aggregateByKey(0)(_+_,_+_)
        ret.foreach{
            case (key,count) => {
                println(s"key: ${key}, count: ${count}")
            }
        }
    }
}
